package domain

import (
	"AcsGo/internal/util"
	"fmt"
)

type ReaderRegisterRequest struct {
	Code                util.Field
	TransactionDate     util.Field
	InstitutionId       util.FieldPrefix
	PatronIdentifier    util.FieldPrefix
	PatronPassword      util.FieldPrefix
	PatronName          util.FieldPrefix
	LibraryId           util.FieldPrefix
	Telno               util.FieldPrefix
	Fixphone            util.FieldPrefix
	Email               util.FieldPrefix
	Address             util.FieldPrefix
	IdCardNo            util.FieldPrefix
	RdrCirctype         util.FieldPrefix
	Deposit             util.FieldPrefix
	ReaderCardIssueDate util.FieldPrefix
	BirDate             util.FieldPrefix
	Ethnicity           util.FieldPrefix
	NativePlace         util.FieldPrefix
	Note                util.FieldPrefix
	EndDate             util.FieldPrefix
	BegDate             util.FieldPrefix
	Gender              util.FieldPrefix
	NewPatronIdentifier util.FieldPrefix
	OperationType       util.FieldPrefix
	Operator            util.FieldPrefix
	PaymentType         util.FieldPrefix
	EducationLevel      util.FieldPrefix
	Position            util.FieldPrefix
}

func ReaderRegister(str string) (*ReaderRegisterRequest, error) {
	if str == "" {
		return nil, fmt.Errorf("str is empty")
	}
	util.Code.Value = "81"

	var query = ReaderRegisterRequest{
		Code:                util.Code,
		TransactionDate:     util.TransactionDate,
		InstitutionId:       util.InstitutionId,
		PatronIdentifier:    util.PatronIdentifier,
		PatronPassword:      util.PatronPassword,
		PatronName:          util.PatronName,
		LibraryId:           util.LibraryId,
		Telno:               util.Telno,
		Fixphone:            util.Fixphone,
		Email:               util.Email,
		Address:             util.Address,
		IdCardNo:            util.IdCardNo,
		RdrCirctype:         util.RdrCirctype,
		Deposit:             util.Deposit,
		ReaderCardIssueDate: util.ReaderCardIssueDate,
		BirDate:             util.BirDate,
		Ethnicity:           util.Ethnicity,
		NativePlace:         util.NativePlace,
		Note:                util.Note,
		EndDate:             util.EndDate,
		BegDate:             util.BegDate,
		Gender:              util.Gender,
		NewPatronIdentifier: util.NewPatronIdentifier,
		OperationType:       util.OperationType,
		Operator:            util.Operator,
		// PaymentType:         util.PaymentType,
		// EducationLevel:      util.EducationLevel,
		// Position:            util.Position,
	}

	length := query.Code.Length + query.TransactionDate.Length
	if len(str) < length {
		return nil, fmt.Errorf("str is too short")
	}

	strTemp := query.Code.GetValue(str)
	strTemp = query.TransactionDate.GetValue(strTemp)
	strTemp = query.InstitutionId.GetValue(strTemp)
	strTemp = query.PatronIdentifier.GetValue(strTemp)
	strTemp = query.PatronPassword.GetValue(strTemp)
	strTemp = query.PatronName.GetValue(strTemp)
	strTemp = query.LibraryId.GetValue(strTemp)
	strTemp = query.Telno.GetValue(strTemp)
	strTemp = query.Fixphone.GetValue(strTemp)
	strTemp = query.Email.GetValue(strTemp)
	strTemp = query.Address.GetValue(strTemp)
	strTemp = query.IdCardNo.GetValue(strTemp)
	strTemp = query.RdrCirctype.GetValue(strTemp)
	strTemp = query.Deposit.GetValue(strTemp)
	strTemp = query.ReaderCardIssueDate.GetValue(strTemp)
	strTemp = query.BirDate.GetValue(strTemp)
	strTemp = query.Ethnicity.GetValue(strTemp)
	strTemp = query.NativePlace.GetValue(strTemp)
	strTemp = query.Note.GetValue(strTemp)
	strTemp = query.EndDate.GetValue(strTemp)
	strTemp = query.BegDate.GetValue(strTemp)
	strTemp = query.Gender.GetValue(strTemp)
	strTemp = query.NewPatronIdentifier.GetValue(strTemp)
	strTemp = query.OperationType.GetValue(strTemp)
	strTemp = query.Operator.GetValue(strTemp)
	strTemp = query.PaymentType.GetValue(strTemp)
	strTemp = query.EducationLevel.GetValue(strTemp)
	_ = query.Position.GetValue(strTemp)
	return &query, nil
}

type ReaderRegisterResponse struct {
	Code             util.Field
	TransactionDate  util.Field
	InstitutionId    util.FieldPrefix
	PatronIdentifier util.FieldPrefix
	JK               util.FieldPrefix
	OK               util.FieldPrefix
	EndDate          util.FieldPrefix
	ScreenMessage    util.FieldPrefix
	PrintLine        util.FieldPrefix
}

func (q ReaderRegisterResponse) StringFormat() (string, error) {
	sb, err := util.ConcatenateFields(q)
	if err != nil {
		return "", err
	}
	return sb, nil
}

func InitReaderRegisterReq(rdbm, ok, jk, endDate string) *ReaderRegisterResponse {
	// 8220240623    182709AO|AA371521199005150927|JK ]] ]] ]] |OK1|XD2051-11-09|AF办理新证成功!|AG办理新证成功!
	util.Code.Value = "82"
	util.TransactionDate.Value = util.GetCurrentTimeInFormat()
	util.PatronIdentifier.Value = rdbm
	util.OK.Value = ok
	if ok == "1" {
		util.ScreenMessage.Value = "办理新证成功!"
		util.PrintLine.Value = "办理新证成功!"
	} else {
		util.ScreenMessage.Value = "办理新证失败!"
		util.PrintLine.Value = "办理新证失败!"
	}
	util.JK.Value = jk
	util.EndDate.Value = endDate

	var queryResponse = ReaderRegisterResponse{
		Code:             util.Code,
		TransactionDate:  util.TransactionDate,
		InstitutionId:    util.InstitutionId,
		PatronIdentifier: util.PatronIdentifier,
		JK:               util.JK,
		OK:               util.OK,
		EndDate:          util.EndDate,
		ScreenMessage:    util.ScreenMessage,
		PrintLine:        util.PrintLine,
	}

	return &queryResponse
}
